package com.cr;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.hibernate.internal.SessionImpl;
import org.primefaces.context.RequestContext;

import com.cr.model.ShopClient;
import com.cr.model.ShopClientItems;
import com.cr.util.HibernateUtil;

@ManagedBean(name = "scr")
@ViewScoped
public class ShopClientRequirementView implements Serializable {

	private static final long serialVersionUID = 1L;
	private String clientEmail;
	private ArrayList<ShopClientItems> clientItems = new ArrayList<ShopClientItems>();
	private String inputItem;

	@PostConstruct
	public void init() {
		SessionImpl session = HibernateUtil.getSession();
		// clientItems.clear();
		// clientEmail=null;
	}

	public void addInputedItemToTable() {

		ShopClientItems shopClientItems = new ShopClientItems();
		shopClientItems.setStaff(inputItem);
		// ShopClient relatedShopClient= new ShopClient();
		// shopClientItems.setRelatedShopClient(relatedShopClient);
		clientItems.add(shopClientItems);
	}

	public String getInputItem() {
		return inputItem;
	}

	public void setInputItem(String inputItem) {
		this.inputItem = inputItem;
	}

	public String saveClientRequirement() {
		SessionImpl session = HibernateUtil.getSession();
		ShopClient shopClient = getShopClient();
		HashSet<ShopClientItems> list = new HashSet<ShopClientItems>();
		for (ShopClientItems shopClientItems : clientItems) {
			shopClientItems.setRelatedShopClient(shopClient);
			list.add(shopClientItems);
		}
		shopClient.setShopClientItems(list);
		session.beginTransaction();
		session.save(shopClient);
		session.getTransaction().commit();
		RequestContext rc = RequestContext.getCurrentInstance();

		rc.showMessageInDialog(new FacesMessage(FacesMessage.SEVERITY_INFO,
				"Items Saved With success", "Items Saved With success"));
		return "";

	}

	private ShopClient getShopClient() {
		Criteria createCriteria = HibernateUtil.getSession().createCriteria(
				ShopClient.class);
		createCriteria.add(Restrictions.eq("", clientEmail));
		Object uniqueResult = createCriteria.uniqueResult();
		if (uniqueResult == null) {
			ShopClient shopClient = new ShopClient();
			shopClient.setEmail(clientEmail);
			return shopClient;
		}

		return (ShopClient) uniqueResult;
	}

	public String getClientEmail() {
		return clientEmail;
	}

	public void setClientEmail(String clientEmail) {
		this.clientEmail = clientEmail;
	}

	public ArrayList<ShopClientItems> getClientItems() {
		return clientItems;
	}

	public void setClientItems(ArrayList<ShopClientItems> clientItems) {
		this.clientItems = clientItems;
	}

}
